import 'package:flutter/material.dart';
import '../../models/order_detail_model.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

class DetailBottomView extends StatelessWidget {
  final DetailInfoModel model;
  final VoidCallback onPay;
  final VoidCallback onCustom;
  final VoidCallback onCancel;
  final VoidCallback onReorder;

  const DetailBottomView({
    super.key,
    required this.model,
    required this.onPay,
    required this.onCustom,
    required this.onCancel,
    required this.onReorder,
  });

  @override
  Widget build(BuildContext context) {
    final localizations = AppLocalizations.of(context)!;
    final buttons = _buildActionButtons(context);
    final hasMultipleButtons = buttons is Row && buttons.children.length > 1;
    
    return SafeArea(
      child: Container(
        color: Colors.white,
        margin: const EdgeInsets.only(bottom: 0),
        padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Divider(height: 1, color: Color(0xFFF5F5F5)),
            const SizedBox(height: 12),
            if (hasMultipleButtons)
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        '${localizations.paymentDue}：',
                        style: TextStyle(
                          fontSize: 15,
                          color: Colors.grey[600],
                        ),
                      ),
                      Text(
                        '${model.symbol}${model.paymentAmount}',
                        style: const TextStyle(
                          fontSize: 15,
                          color: Colors.red,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 12),
                  buttons,
                  const SizedBox(height: 15),
                ],
              )
            else
              Row(
                children: [
                  Text(
                    '${localizations.paymentDue}：',
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.grey[600],
                    ),
                  ),
                  Text(
                    '${model.symbol}${model.paymentAmount}',
                    style: const TextStyle(
                      fontSize: 15,
                      color: Colors.red,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const Spacer(),
                  buttons,
                ],
              ),
              if (!hasMultipleButtons)
              const SizedBox(height: 15),
          ],
        ),
      ),
    );
  }

  Widget _buildActionButtons(BuildContext context) {
    final localizations = AppLocalizations.of(context)!;
    switch (model.status) {
      case 0:
        return Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            _buildBorderButton(isChineseLocale() ? '${localizations.cancel}${localizations.order}' : localizations.cancel, onCancel),
            const SizedBox(width: 12),
            _buildFilledButton(localizations.contactCustomer, onCustom),
            const SizedBox(width: 12),
            _buildFilledButton(localizations.goPay, onPay),
          ],
        );
      case 1:
        return Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            _buildFilledButton(localizations.contactCustomer, onCustom),
            const SizedBox(width: 12),
            _buildFilledButton(localizations.shipment, onCustom),
          ],
        );
      case 3:
      case 5:
      case 8:
        return _buildBorderButton(localizations.orderAgain, onReorder);
      case 9:
        return _buildBorderButton(localizations.orderAgain, onReorder);
      default:
        return const SizedBox.shrink();
    }
  }

  Widget _buildFilledButton(String text, VoidCallback onPressed) {
    return SizedBox(
      height: 34,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFF5D5FEE),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(17),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 12), // 添加水平内边距

        ),
        onPressed: onPressed,
        child: Text(
          text,
          style: const TextStyle(color: Colors.white),
        ),
      ),
    );
  }

  Widget _buildBorderButton(String text, VoidCallback onPressed) {
    return SizedBox(
      height: 34,
      child: OutlinedButton(
        style: OutlinedButton.styleFrom(
          side: const BorderSide(color: Color(0xFF5D5FEE)),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(17),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 12), // 添加水平内边距
        ),
        onPressed: onPressed,
        child: Text(
          text,
          style: const TextStyle(color: Color(0xFF5D5FEE)),
        ),
      ),
    );
  }

}